package com.ctsi.ssdc.service.impl;


import org.springframework.stereotype.Service;
import org.apache.commons.collections.CollectionUtils;
import com.ctsi.ssdc.domain.LitemallCollect;
import com.ctsi.ssdc.domain.LitemallCollectExample;
import com.ctsi.ssdc.service.LitemallCollectService;
import com.ctsi.ssdc.repository.LitemallCollectRepository;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Objects;
import com.ctsi.ssdc.service.impl.StrengthenBaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import com.ctsi.ssdc.model.PageResult;
import org.springframework.data.domain.Pageable;
import com.github.pagehelper.PageHelper;
/**
 * Service Implementation for managing LitemallCollect.
 *
 * @author ctsi-biyi-generator
 *
 */
@Service
public class LitemallCollectServiceImpl 
	extends StrengthenBaseServiceImpl<LitemallCollectRepository, LitemallCollect, Integer, LitemallCollectExample> 
	implements LitemallCollectService {

    @Autowired
    LitemallCollectRepository litemallCollectRepository;


    /**
    * GET  /litemallCollects : get the litemallCollects firstStringBaseColumn.
    */
    @Override
    public PageResult<LitemallCollect> findFirstStringColumn(String str,Pageable pageable){
        if (Objects.nonNull(pageable)) {
            PageHelper.startPage(pageable.getPageNumber() + 1, pageable.getPageSize());
        }
        LitemallCollectExample litemallCollectExample = new LitemallCollectExample();
        String orderBy = getPageOrderBy(pageable);
        if(StringUtils.isNotEmpty(orderBy)) {
            litemallCollectExample.setOrderByClause(orderBy);
        }
        if (StringUtils.isEmpty(str)){
            litemallCollectExample.createCriteria();
        } 
        List<LitemallCollect>  data = litemallCollectRepository.selectByExample(litemallCollectExample);

        long count = 0L;
        if (CollectionUtils.isNotEmpty(data)){
            count = litemallCollectRepository.countByExample(litemallCollectExample);
        }
        return new PageResult<LitemallCollect>(data,count,count);
    }

    @Override
    public List<LitemallCollect> queryByType(Integer userId, Byte type, Integer page, Integer limit, String sort, String order) {
        LitemallCollectExample example = new LitemallCollectExample();
        LitemallCollectExample.Criteria criteria = example.createCriteria();

        if (type != null) {
            criteria.andTypeEqualTo(type);
        }
        criteria.andUserIdEqualTo(userId);
        criteria.andDeletedEqualTo(false);

        if (!org.springframework.util.StringUtils.isEmpty(sort) && !org.springframework.util.StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, limit);
        return litemallCollectRepository.selectByExample(example);
    }

    @Override
    public LitemallCollect queryByTypeAndValue(Integer userId, Byte type, Integer valueId) {
        LitemallCollectExample example = new LitemallCollectExample();
        example.or().andUserIdEqualTo(userId).andValueIdEqualTo(valueId).andTypeEqualTo(type).andDeletedEqualTo(false);
        return litemallCollectRepository.selectOneByExample(example);
    }

    @Override
    public void deleteById(Integer id) {
        litemallCollectRepository.logicalDeleteByPrimaryKey(id);
    }

    @Override
    public int add(LitemallCollect collect) {
        collect.setAddTime(ZonedDateTime.now());
        collect.setUpdateTime(ZonedDateTime.now());
        return litemallCollectRepository.insertSelective(collect);
    }

    @Override
    public int count(Integer userId, byte b, Integer gid) {
        LitemallCollectExample example = new LitemallCollectExample();
        example.or().andUserIdEqualTo(userId).andTypeEqualTo(b).andValueIdEqualTo(gid).andDeletedEqualTo(false);
        return (int) litemallCollectRepository.countByExample(example);
    }

    private String getPageOrderBy(Pageable page) {
        if(page!= null && page.getSort() != null) {
            StringBuilder sb = new StringBuilder();
            page.getSort().forEach(sort -> sb.append(sort.getProperty())
            .append(" ").append(sort.getDirection()).append(","));
            if(sb.length() > 1) {
                return (sb.substring(0,sb.length()-1));
             }
        }
        return null;
    }
}
